package sctu.edu.java.year2016.team08.lisonghong.day20160616.chapter11_3;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 松红 on 2016/6/16.
 * 创建Computer类，该类中有一个计算最大公约数的方法，如果向该方法传递负整数，该方法就会抛出自定义异常
 */
public class Computer extends MyException{
    public int i;
    public int j;
    public void divisor(int a, int b) {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> listmax = new ArrayList<Integer>();
        if (a % b == 0) {
            System.out.println("最大公约数为：" + b);
        } else if (b % a == 0) {
            System.out.println("最大公约数为：" + a);
        } else if (a % b != 0 && b % a != 0) {
            for (i = 1; i <= a; i++, j++) {
                if (a % i == 0) {

                    System.out.println(a + "的约数有：" + i);
                    list.add(i);
                }
            }
            for (int j = 1; j <= b; j++) {
                if (b % j == 0) {

                    System.out.println(b + "的约数有：" + j);

                    list1.add(j);
                }
            }
        }
        for (int i = list.get(list.size() - 1); i > 0; i--) {
            for (int j = list1.get(list1.size() - 1); j > 0; j--) {
                if (i == j) {
                    listmax.add(i);
                }


            }
        }
        System.out.println("最大公约数为：" + listmax.get(0));
    }

    public static void main(String[] args) {
        try {
            Computer computer = new Computer();
            computer.divisor(10, 6);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();

        }
    }
}

